package com.heima.kafka.sample;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

public class ProducerQuickStart {
    /**
     * kafka快速入门 - 生产者
     * @param args
     */
    public static void main(String[] args) {
        /*
        生产者
         */
        //1.kafka链接配置信息
        Properties properties = new Properties();

        //kafka链接地址
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.86.128:9092");
        //消息的反序列化器
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        //2.创建生产者对象
        KafkaProducer<String,String> producer = new KafkaProducer<String, String>(properties);

        //3.发送消息
//        ProducerRecord<String, String> record = new ProducerRecord<>("topic-first", "key-001", "hello world");
//        producer.send(record);

        for (int i = 0; i < 10; i++) {
            ProducerRecord<String, String> record = new ProducerRecord<>("itcast-topic-input","hello world");
            producer.send(record);
        }

        //4.关闭信息通道 ,必需关闭，否则发送消息不成功
        producer.close();



    }
}
